package com.example.xui.BlankFragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xui.R;
import com.mob.MobSDK;
import com.xuexiang.xui.widget.edittext.verify.VerifyCodeEditText;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;


public class BlankFragment3 extends Fragment {
    private TextView textView;
    BlankFragment4 f4=new BlankFragment4();
    private FragmentManager manager;
    private FragmentTransaction ft;
    private VerifyCodeEditText button;
    static String number;
    private TextView phone1;
    private EventHandler handler;
    private String phone;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank3, container, false);
    }

    public void onActivityCreated(@Nullable Bundle saveInstanceState) {
        super.onActivityCreated(saveInstanceState);
        MobSDK.init(getContext(), "30758f83a16a2","9a26dba5151c666c0e99069902791c81");
       textView=getActivity().findViewById(R.id.textView6);
       button=getActivity().findViewById(R.id.vcet_1);
       phone1=getActivity().findViewById(R.id.textView4);
       Bundle bundle=getArguments();
       if(bundle!=null){
           phone=bundle.getString("phone");
       }
       phone1.setText("+86 "+phone);
        manager=getFragmentManager();
      TextView textView=getActivity().findViewById(R.id.textView6);
      textView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              play();
          }
      });
      Button button=getActivity().findViewById(R.id.but_jiao);
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              tijiao();
          }
      });
       modSdk();
    }

    public void modSdk(){
        handler = new EventHandler(){
            @Override
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE){
                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码成功
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getContext(),"验证成功",Toast.LENGTH_SHORT).show();
                                Bundle bundle=new Bundle();
                                bundle.putString("phone",phone);
                                f4.setArguments(bundle);
                                ft=manager.beginTransaction();
                                ft.replace(R.id.framelayout3,f4);
                                ft.addToBackStack(null);
                                ft.commit();
                                ConstraintLayout constraintLayout=getActivity().findViewById(R.id.constarintlayout2);
                                constraintLayout.setVisibility(View.INVISIBLE);
                           }
                        });

                    }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                        //获取验证码成功
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getContext(),"验证码已发送",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
                    }
                }else{
                    ((Throwable)data).printStackTrace();
                    Throwable throwable = (Throwable) data;
                    try {
                        JSONObject obj = new JSONObject(throwable.getMessage());
                        final String des = obj.optString("detail");
                        if (!TextUtils.isEmpty(des)){
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getContext(),"提交错误信息", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }
        };

        SMSSDK.registerEventHandler(handler);
    }

    public void tijiao() {
        number =button.getInputValue().toString();
       // String phone=editText.getText().toString();
        Log.i("Resiter.class","msg1="+number);
        Toast.makeText(getContext(),"number="+number,Toast.LENGTH_SHORT).show();
        SMSSDK.submitVerificationCode("86",phone,number);
    }
    public void play() {
        //获取验证码
       // String phone=editText.getText().toString();
        Log.i("Resiter.class","msg1="+phone);
        SMSSDK.getVerificationCode("86",phone);
    }

   @Override
    public void onStart() {
        super.onStart();
        ConstraintLayout constraintLayout1=getActivity().findViewById(R.id.constarintlayout2);
        constraintLayout1.setVisibility(View.VISIBLE);
        ConstraintLayout constraintLayout=getActivity().findViewById(R.id.constraintlayout1);
        constraintLayout.setVisibility(View.INVISIBLE);
     /*   ConstraintLayout constraintLayout2=getActivity().findViewById(R.id.constraintLayout3);
        constraintLayout2.setVisibility(View.INVISIBLE);*/
    }
    @Override
    public void onResume() {
        super.onResume();
        Toast.makeText(getContext(),"onResour1",Toast.LENGTH_SHORT).show();
        Log.i("BlankFragment.this","onResume");
        ConstraintLayout constraintLayout=getActivity().findViewById(R.id.constraintlayout1);
        ConstraintLayout constraintLayout1=getActivity().findViewById(R.id.constarintlayout2);
        // constraintLayout1.setVisibility(View.VISIBLE);
        if(constraintLayout.getVisibility()==View.INVISIBLE){
            constraintLayout1.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        ConstraintLayout constraintLayout=getActivity().findViewById(R.id.constraintlayout1);
        constraintLayout.setVisibility(View.VISIBLE);
        ConstraintLayout constraintLayout1=getActivity().findViewById(R.id.constarintlayout2);
        constraintLayout1.setVisibility(View.INVISIBLE);
      //  Toast.makeText(getContext(),"onPause",Toast.LENGTH_SHORT).show();
    }
}
